import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';

import { systemid } from '../atom/config';

interface Message {
	filter: {
		doccode: string;
	};
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	const { exec: e2, prepare: p2 } = query(action_id, session_id, systemid);
	p2('buffet_orders', ['_id', 'doccode', 'storage_time', 'docdate', 'docstatus', 'equip_no', 'equip_name', 'location_no', 'location_name', 'companyid', 'companyname', 'commodityid', 'commodity_name', 'account_sum', 'copy_single_way_no'], { doccode: message.filter.doccode }, 200, 1, [], []);
	const [data] = await e2();
	const d = data[data.length - 1];
	const docstatus = '300';
	const { exec, prepare } = update(action_id, session_id, msg.systemid);
	prepare('buffet_orders', [{ _id: d._id, docstatus }]);
	const ret = await exec();
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
